package com.amado.easyui2.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.amado.easyui2.constant.GlobalConstants;
import com.amado.easyui2.dao.UserDao;
import com.amado.easyui2.domain.User;
import com.amado.easyui2.enums.ApiErrorEnum;
import com.amado.easyui2.exception.ApiException;
import com.amado.easyui2.service.UserService;
import com.amado.easyui2.util.Md5Util;
import com.amado.easyui2.vo.Page;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;

	@Override
	public boolean add(User user) {
		if (user == null) {
			throw new ApiException(ApiErrorEnum.ERROR_100);
		}
		//判断是否存在
		User returnUser = userDao.getBy(user.getUserName());
		if(returnUser!=null) {
			throw new ApiException(ApiErrorEnum.ERROR_1001);
		}
		//用户名不存在，做添加逻辑
		userDao.add(user);
		return true;
	}

	@Override
	public Page<User> listAll(String name, String departmentNum, Page page) {
		int total = userDao.listAllCount(name, departmentNum);
		Page<User> newPage = new Page<>();
		newPage.setTotal(total);
		newPage.setRows(userDao.listAll(name, departmentNum, page));
		return newPage;
	}

	@Override
	public boolean delete(Long id) {
		//删除时查询一下是否为管理员，如果是不允许删除 
		User user = userDao.getById(id);
		if(user==null) {
			throw new ApiException("0","未找到用户");
		}
		//是管理员
		if(GlobalConstants.Y.equals(user.getManage())) {
			throw new ApiException("0","管理员账号不允许删除");
		}
		if (id != null) {
			userDao.delete(id);
		}
		return false;
	}
	
	@Override
	public boolean delete(Long[] ids) {
		for(Long tempId:ids) {
			delete(tempId);
		}
		return true;
	}

	@Override
	public boolean update(User user) {
		if (user != null) {
			userDao.update(user);
			return true;
		}
		return false;
	}

	@Override
	public List<User> list() {
		return userDao.list();
	}

//	@Override
//	public List<User> getUserName(User user) {
//		if(user!=null) {
//			userDao.login(user);	
//		}
//		return null;
//	}

	@Override
	public User getBy(String userName) {
		// TODO Auto-generated method stub
		return userDao.getBy(userName); 
	}

	@Override
	public boolean updatePassword(String userName, String oldPassword, String newPassword) {
		//1、校验旧密码,失败返回false
		//1.1 根据用户名查询出用户信息
		User user = userDao.getBy(userName);
		//1.2 拿到用户信息中的密码与旧密码比较是否相等
		if(!user.getPassword().equals(Md5Util.encode(oldPassword))) {
			return false;
		}
		//2、修改密码 ，修改完返回true,update t_user set password =新密码 where username=用户名
		if(userDao.updatePassword(userName, Md5Util.encode(newPassword))==1) {
			return true ;
		}
		return false;
	}

	@Override
	public List<User> getUserName(User user) {
		if (user != null) {
			userDao.login(user);
		}
		return null;
	}

	@Override
	public void getByUserName(String userName) {
		if(userName!=null) {
			return ;
		}
	}
}
